package classes;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class HomeworkManagerStudent {

	static Statement statement;
	public static String HOMEWORKMANAGERSTUDENT = "homeworkManagerStudent";

	public HomeworkManagerStudent(Statement statement) {
		this.statement = statement;
	}

	public void createHomework(String homeworkName, int studentID, String file,
			String comment, double mark, String doneStatus, String checkedStatus) {
		try {
			statement
					.executeUpdate("insert into homeworkForStudents values (0, '"
							+ homeworkName
							+ "', "
							+ studentID
							+ ", '"
							+ file
							+ "', '"
							+ comment
							+ "', "
							+ mark
							+ ", '"
							+ doneStatus + "', '" + checkedStatus + "')");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public HomeWorkStudent getHomeWorkStudent(int student_id, int homework_Id) {
		try {
			ResultSet set = statement
					.executeQuery("Select * from homeworkforstudents where student_id ="
							+ student_id + " and homework_id = " + homework_Id);
			set.next();
			String homeworkName = set.getString("homework_name");
			String fileNameStudent = set.getString("fileName_student");
			double mark = (double) set.getInt("mark");
			String comment = set.getString("comment");
			String uploadTime = set.getString("upload_time");
			String done = set.getString("done_status");
			String checkStatus = set.getString("checked_status");
			HomeWorkStudent homework = new HomeWorkStudent(homework_Id,
					homeworkName, student_id, fileNameStudent, uploadTime,
					comment, mark, done, checkStatus);
			return homework;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public HomeWorkStudent[] getStudentHomework(int homework_id) {
		try {
			// System.out.println(homework_id);
			ResultSet set = statement
					.executeQuery("select *from homeworkforstudents where homework_id="
							+ homework_id);
			set.last();
			HomeWorkStudent[] works = new HomeWorkStudent[set.getRow()];
			set.beforeFirst();
			for (int i = 0; i < works.length; i++) {
				set.next();
				int student_id = set.getInt("student_id");
				String homeworkName = set.getString("homework_name");
				String fileName = set.getString("fileName_student");
				String uploadTime = set.getString("upload_time");
				String comment = set.getString("comment");
				double mark = set.getDouble("mark");
				String done_staus = set.getString("done_status");
				String checked_status = set.getString("checked_status");
				HomeWorkStudent hm = new HomeWorkStudent(homework_id,
						homeworkName, student_id, fileName, uploadTime,
						comment, mark, done_staus, checked_status);
				works[i] = hm;
			}
			return works;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static void updateSomething(int homeworkID, String columnName,
			String value) {
		try {
			statement.executeUpdate("Update homeworkforstudents set "
					+ columnName + "='" + value + "' where homework_id="
					+ homeworkID + ";");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
